package com.example.liyuehui.paging.adapter;

import android.arch.paging.PagedListAdapter;
import android.support.annotation.NonNull;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.liyuehui.paging.R;
import com.example.liyuehui.paging.datasource.TextDataSourceFactory;
import com.example.liyuehui.paging.holder.NetworkStateItemViewHolder;
import com.example.liyuehui.paging.holder.TextViewViewHolder;


public class PagingAdapter extends PagedListAdapter<String, RecyclerView.ViewHolder> {

	private static final int NORMAL = 0;
	private static final int BOTTOM = 1;
	TextDataSourceFactory.NetStatus netStatus;
	private NetworkStateItemViewHolder networkStateItemViewHolder;

	public PagingAdapter(@NonNull DiffUtil.ItemCallback<String> diffCallback) {
		super(diffCallback);
	}

	@NonNull
	@Override
	public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
		if (viewType == BOTTOM) {
			View view = LayoutInflater.from(parent.getContext())
					.inflate(R.layout.network_state_item, parent, false);
			networkStateItemViewHolder = new NetworkStateItemViewHolder(view);
			return networkStateItemViewHolder;
		}
		View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.text, parent, false);
		return new TextViewViewHolder(view);
	}

	@Override
	public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
		if (holder instanceof TextViewViewHolder) {
			TextViewViewHolder textViewViewHolder = (TextViewViewHolder) holder;
			textViewViewHolder.tv.setText(getItem(position));
		} else if (holder instanceof NetworkStateItemViewHolder) {
			NetworkStateItemViewHolder networkStateItemViewHolder = (NetworkStateItemViewHolder) holder;
			if (this.netStatus == TextDataSourceFactory.NetStatus.BOTTOM) {
				networkStateItemViewHolder.errorMsg.setText("到底了 ");
				networkStateItemViewHolder.retry.setVisibility(View.GONE);
			} else {
				networkStateItemViewHolder.errorMsg.setText("loading。。。。");
				networkStateItemViewHolder.retry.setVisibility(View.GONE);
			}
		}
	}


	public void setNetStatus(TextDataSourceFactory.NetStatus status) {
		TextDataSourceFactory.NetStatus pre = this.netStatus;
		boolean had = hasExtraRow();
		netStatus = status;
		boolean has = hasExtraRow();
		if (had != has) {
			int count = super.getItemCount();
			if (had) {
				notifyItemRemoved(count);
				int count1 = super.getItemCount();
			} else {
				notifyItemInserted(super.getItemCount());
				int count1 = super.getItemCount();
			}

		} else {
			notifyItemChanged(getItemCount() - 1);
		}

//		ViewGroup.LayoutParams layoutParams = networkStateItemViewHolder.itemView.getLayoutParams();
//		if (status == TextDataSourceFactory.NetStatus.LOADING) {
//			Context context = networkStateItemViewHolder.itemView.getContext();
//			DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
//			layoutParams.height = displayMetrics.heightPixels;
//			notifyItemInserted(super.getItemCount() - 1);
//		} else if (status == TextDataSourceFactory.NetStatus.LOAD_END) {
//			if (pre == TextDataSourceFactory.NetStatus.INIT) {
//				notifyItemRemoved(super.getItemCount() - 1);
//			} else {
//
//			}
//		}
//		networkStateItemViewHolder.itemView.setLayoutParams(layoutParams);
	}

	private boolean hasExtraRow() {
		if (netStatus != null && netStatus != TextDataSourceFactory.NetStatus.LOAD_END) {
			return true;
		}
		return false;
	}

	@Override
	public int getItemViewType(int position) {
		if (hasExtraRow() && position == (getItemCount() - 1)) {
			return BOTTOM;
		}
		return NORMAL;
	}

	@Override
	public int getItemCount() {
		if (hasExtraRow()) {
			return super.getItemCount() + 1;
		}
		return super.getItemCount();
	}

}
